﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class PTArrangeCheck : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new System.ArgumentNullException();
			}
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				IList<FamilyInstance> list = new List<FamilyInstance>();
				SprinklerFilter sprinklerFilter = new SprinklerFilter();
				int num = 0;
				transaction = new Transaction(document, "消防设施范围检查");
				transaction.Start();
				for (;;)
				{
					if (num != 0)
					{
						list.Clear();
						RevitVersionFuncs.ClearSelection(selection);
					}
					num++;
					if (selection.GetElementIds().Any<ElementId>())
					{
						foreach (ElementId elementId in selection.GetElementIds())
						{
							bool flag = false;
							Element elementById = document.GetElementById(elementId);
							if (elementById is FamilyInstance)
							{
								FamilyInstance familyInstance = elementById as FamilyInstance;
								int integerValue = elementById.Category.Id.IntegerValue;
								if (integerValue == -2008099)
								{
									flag = true;
								}
								if (integerValue == -2008085)
								{
									flag = true;
								}
								if (integerValue == -2001140 && (familyInstance.Symbol.Family.Name.Contains("消火栓") || familyInstance.Symbol.Family.Name.Contains("消防")))
								{
									flag = true;
								}
								if (flag)
								{
									list.Add(elementById as FamilyInstance);
								}
							}
						}
					}
					if (list.Count <= 0)
					{
						foreach (Element element in selection.PickElementsByRectangle(sprinklerFilter, "请框选要检查范围的消防设施……"))
						{
							list.Add(element as FamilyInstance);
						}
					}
					if (list.Count > 0)
					{
						int num2 = 1;
						bool flag2 = true;
						bool flag3 = true;
						new List<ElementId>();
						foreach (FamilyInstance familyInstance2 in list)
						{
							if (!YJKRevitTools.ShareParaBeExist(familyInstance2, "喷淋半径") && !YJKRevitTools.ShareParaBeExist(familyInstance2, "保护半径"))
							{
								flag2 = false;
							}
							else
							{
								Parameter parameter = familyInstance2.GetParameter("喷淋半径");
								Parameter parameter2 = familyInstance2.GetParameter("保护半径");
								if (parameter == null && parameter2 == null)
								{
									flag3 = false;
								}
								else
								{
									double num3 = (parameter != null) ? parameter.AsDouble() : parameter2.AsDouble();
									num3 = num3 * (double)YJKRevitTools.GetViewPlanScale(document) / 100.0;
									new AuxiliaryLineOperate().DrawRangLine(familyInstance2, num3, "PTArrangeCheck", num2);
									num2++;
								}
							}
						}
						if (!flag2)
						{
							YJKMessageBox.Warning("检查失败，某些消防设施没有喷淋半径或保护半径参数");
						}
						if (!flag3)
						{
							YJKMessageBox.Warning("检查失败，某些消防设施的喷淋半径或保护半径为空");
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if ((int)status == 1)
					{
						transaction.Commit();
					}
					else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
